#pragma once
#include "ISomeContainterIterator.h"
#include <boost/atomic.hpp>
class IObject;

class CSomeContainer
{
    struct Node
    {
        boost::shared_ptr<IObject> data;
        int index;
        Node* next;
        Node(boost::shared_ptr<IObject> d, int ind, Node* n)
            : data(d)
            , index(ind)
            , next(n)
        {}
//         ~Node()
//         {
//             std:: cout << "d " << index << std::endl;
//         }
    };
public:
    CSomeContainer();
    ~CSomeContainer();

    bool Register(int objectId, boost::shared_ptr<IObject> object);
    bool Unregister(int objectId);
    boost::shared_ptr<IObject> Query(int objectId);

    std::auto_ptr<ISomeContainterIterator> CreateIterator();

private:
    void ChainNodeList(Node* first, Node* last);
    void ChainNodeList(Node* nodes);
    void ChainOneNode(Node* node);
    void ChainPendingNodes(Node* first, Node* last);
    static void DeleteNodes(Node* nodes);
    void TryReclaim(Node* oldHead);

private:
    friend class CSomeContainterIterator;
    boost::atomic<Node*> m_head;
    boost::atomic<Node*> m_toBeDeleted;
    boost::atomic<unsigned> m_threadInPop;
    boost::atomic<size_t> m_size;
};
